﻿using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Jungle.Code.Utils;
using Jungle.Code.DAL;


namespace Jungle.Prodamo.MostNeedFunctions
{
    public class Functions
    {
        public static string tLeft;

        // Функция возращающая строку оставшегося времени
        public static string GetTimeLeft(DateTime endDate)
        {
            tLeft = "";
            DateTime nowDate = DateTime.Now;
            TimeSpan leftTime = new TimeSpan();

            leftTime = endDate - nowDate;

            if (leftTime.Days > 0)
            {
                tLeft += leftTime.Days.ToString() + " дней ";
                if (leftTime.Hours > 0) tLeft += leftTime.Hours.ToString() + "ч ";
            }
            else
            {
                if (leftTime.Hours > 0)
                {
                    tLeft += leftTime.Hours.ToString() + "ч ";
                    if (leftTime.Minutes > 0) tLeft += leftTime.Minutes.ToString() + "м ";
                }
                else
                {
                    if (leftTime.Minutes > 0) tLeft += leftTime.Minutes.ToString() + "м ";
                    if (leftTime.Seconds > 0) tLeft += leftTime.Seconds.ToString() + "c ";
                }
            }

            return tLeft;
        }

        // Функция получения имени пользователя по его id
        public string GetUserName(int id_user)
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("mdUsers_GetById", connect);
                SqlParameter param_id_user = command.Parameters.Add("@userId", SqlDbType.Int);
                SqlDataReader reader = null;

                command.CommandType = CommandType.StoredProcedure;
                param_id_user.Value = id_user;
                command.Connection.Open();
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    string linkUserInfo = "<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/userinfo.aspx?uId=" + reader.GetValue(0).ToString() + "\"> " + reader.GetValue(1).ToString().Trim() + "</a>";
                    return linkUserInfo;                
                }
                else return "noname";
                
                reader.Close();
                command.Connection.Close();
            }
        }

        // Функция MD5 коддер
        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        public static string GetTimeWaitLeft(DateTime endDate)
        {
            tLeft = "";
            DateTime nowDate = DateTime.Now;
            TimeSpan leftTime = new TimeSpan();

            leftTime = endDate.AddDays(3) - nowDate;

            if (leftTime.Days > 0)
            {
                tLeft += leftTime.Days.ToString() + " дней ";
                if (leftTime.Hours > 0) tLeft += leftTime.Hours.ToString() + "ч ";
            }
            else
            {
                if (leftTime.Hours > 0)
                {
                    tLeft += leftTime.Hours.ToString() + "ч ";
                    if (leftTime.Minutes > 0) tLeft += leftTime.Minutes.ToString() + "м ";
                }
                else
                {
                    if (leftTime.Minutes > 0) tLeft += leftTime.Minutes.ToString() + "м ";
                    if (leftTime.Seconds > 0) tLeft += leftTime.Seconds.ToString() + "c ";
                }
            }

            return tLeft;
        }
    }

}
